<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="net.netca.bmps.userservice.service.UserService"%>
<%@page import="net.netca.bmps.userservice.servicelayer.UserServiceImpl"%>
<%@page import="net.netca.bmps.userservice.bo.*"%>
<%@page import="java.util.Date"%>
<%@page import="net.netca.gdltax.bpms.businessmanage.utilits.*"%>
<%@page import="net.netca.util.encoders.Base64"%>
<%@page import="java.security.cert.X509Certificate"%>
<%@page import="net.netca.bmps.userservice.bo.CertInfoLocal"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<% 
	 response.setHeader("Cache-Control", "no-cache");
     response.setHeader("Pragma", "no-cache");
     response.setDateHeader("Expires", 0);


	
	UserService userService=null;
	CertInfo wscertinfo=null;
	CertInfoLocal certinfo=new CertInfoLocal();
    String action=null;
    int caid=-1;
    String dns=null;
    Date tmfm=null;
    Date tmto=null;

	String reqno=null;
	String strCaname=null;
	try{
		userService=new UserServiceImpl();
		
	    request.setCharacterEncoding("UTF-8");
	    action=request.getParameter("action");
	  
	    reqno=request.getParameter("reqno");
	    if(reqno!=null)
	    	reqno=reqno.trim();
	    strCaname=request.getParameter("caname");
	    //strCaname=userService.getCanameByReqno("20110616000009");
	    if(strCaname!=null)
	    	strCaname=strCaname.trim();
	    if(action!=null&&"query".equalsIgnoreCase(action)){
	    	wscertinfo=(CertInfo)request.getSession().getAttribute("certlist");
	    	if(wscertinfo==null){
	    		throw new Exception("没有找到对应的证书信息");
	    	}
	    	//BeanUtils.copyProperties(certinfo,wscertinfo);
	    	Converter.CertInfoConvert(certinfo,wscertinfo);
	    	if(certinfo.getSigncert()!=null){
	    		certinfo.setX509SignCert(userService.getNetCACert(certinfo.getSigncert()));
	    	}
	    	if(certinfo.getEnccert()!=null){
	    		certinfo.setX509EncCert(userService.getNetCACert(certinfo.getEnccert()));
	    	}
	    }
	}catch(Exception e){
		e.printStackTrace();
		request.getSession().setAttribute("exception", e);
		response.sendRedirect("showerror.jsp");
	}
%>


<html>
<head>
<script type="text/javascript" src="<%=basePath%>dwr/util.js"></script>
<script type="text/javascript" src="<%=basePath%>dwr/engine.js"></script>
<script type="text/javascript" src="<%=basePath%>dwr/interface/UserServiceImpl.js"></script>
<link href="./jsp/resource/css/netca2.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


<script type="text/javascript">
	function getTypeAndSerial(caname){
		var obj;
	try{
		obj = new ActiveXObject("NetcaBpmsClientCom.PKI");
		if(obj.IsInstalled(caname)){
			obj.SetCA(caname);
		if(obj.KeyCount>1){
				alert("只能插入1个key");
				return false;
		}
		//obj.Connect();
		document.getElementById("type").value=obj.Type;
		//alert(obj.Type);
		document.getElementById("serialnum").value=obj.SerialNumber;
		//alert(obj.SerialNumber);
		return true;
		}else{
			alert("不支持的ca!")
			return false;
		}
	}catch(e){
		alert(e.message);
		return false;
	}
	}
	
	function checkkey(){
		//设置ajax为同步
		DWREngine.setAsync(false);
		//检查reqno
		var reqnoVal;
		if($("#reqno").val()==""){
			alert("请填写受理单号!");
			return false;
		}else{
			reqnoVal=$("#reqno").val();
		}
		
		//获取caname的值
		UserServiceImpl.getCanameByReqno(reqnoVal,dealCaname);
		var canameVal=$("#caname").val();
		if(canameVal==null||canameVal==""){
			alert("没有该受理号对应的CA");
			return false;
		}
		if(false==getTypeAndSerial(canameVal)){
			return false;
		}
		return true;
	}

	function install(enccert,signcert,enckeypair,checkid){
		
		DWREngine.setAsync(false);
		
		var a="change"+checkid;

		var trId="tr"+checkid;
	
			var obj;
			
	try{
		var canameVal=$("#caname").val();
		alert(canameVal);
		obj = new ActiveXObject("NetcaBpmsClientCom.PKI");
		if(obj.IsInstalled(canameVal)){
			obj.SetCA(canameVal);
		if(obj.KeyCount>1){
				alert("只能插入1个key");
				return false;
		}
		
		var el=$("#"+trId); 
		
		//alert(2);
		//不为空更换密钥
		if(enckeypair!=null&&enckeypair!=""){
			obj.InstallCertAndPrivKey(enccert,signcert,enckeypair);
		}else{
			obj.InstallCert(enccert,signcert);
		}
		//这里更改状态
		UserServiceImpl.updateInstall(1,checkid,callback);
		
		//隐藏安装成功的tr
		el.hide();
		
		return true;
		}
	}catch(e){
		alert(e.message);
		return false;
	}
	}

	function callback(){
		alert("安装成功！");
	}
	
	function dealCaname(canameVal){
		
			
		$("#caname").val(canameVal);
	}
</script>
	

<title>证书安装</title>
</head>
<body>
	<h4>请先插入需要安装证书的KEY再点击查询</h4>
	<form name="form1" id="form1" action="servlet/QueryPersonalCertSlt" method="post">
	<table class="tablestyle">	
	<tr>
		<td>请输入受理单号:</td>
		<td>
			<input type="text"  name="reqno" id="reqno" value="<%=reqno==null?"":reqno%>" >
			<input type="hidden"  name="caname" id="caname" value="<%=strCaname==null?"":strCaname%>">
		</td>
	</tr>
	
		
	</table>
	<input type="hidden" id="action" name="action" value="query">
	<input type="hidden" id="type" name="type">
	<input type="hidden" id="serialnum" name="serialnum">
	
	<input class="colorCCC2" type="submit" id="query" name="query" value="查询" onclick="return checkkey()">
	
	</form>
	
	<table class="tablestyle">
	<tr>
		<td>通用名称</td>
		<td>部门名称</td>
		<td>单位名称</td>
		<td>Email</td>
		<td>申请时间</td>
		<td>安装</td>
	</tr>
	<%	
		
		String dn=null;
		
		
	if(certinfo!=null){
			if(certinfo.getX509SignCert()!=null||certinfo.getX509EncCert()!=null){
				X509Certificate cert=null;
				if(certinfo.getX509SignCert()!=null)
					cert=certinfo.getX509SignCert().ToX509Certificate();
				else
					cert=certinfo.getX509EncCert().ToX509Certificate();
			dn=cert.getSubjectDN().getName();
			byte[] bsigncert=certinfo.getX509SignCert().getEncodeValue();
			byte[] benccert=certinfo.getX509EncCert().getEncodeValue();
			byte[] bencpair=certinfo.getEnckeypair();
			String pemsigncert=Base64.encodeToString(bsigncert,0,bsigncert.length,Base64.BASE64_FLAG_NOCRLF);
			String pemenccert=Base64.encodeToString(benccert,0,benccert.length,Base64.BASE64_FLAG_NOCRLF);
		
			String encpair=null;	
			if(bencpair==null||bencpair.length==0)
				encpair="";
			else
				encpair=Base64.encodeToString(bencpair,0,bencpair.length,Base64.BASE64_FLAG_NOCRLF);
	%>
		<tr id="tr<%=certinfo.getId()%>">
			<td><%=SecurHelper.GetCnFromDn(dn)%></td>
			<td><%=SecurHelper.GetOUFromDn(dn)%></td>
			<td><%=SecurHelper.GetOFromDn(dn)%></td>
			<td><%=SecurHelper.GetEMAILADDRESSFromDn(dn)%></td>
			<td><%=StrHelper.GetDateTimeStr(certinfo.getReqtime().getTime()) %></td>
			<td><a href="#" onclick="install('<%=pemenccert%>','<%=pemsigncert%>','<%=encpair%>','<%=certinfo.getId()%>')"><font color="blue">安装</font></a></td>
			<input type="hidden" id="change<%=certinfo.getId()%>">
		</tr>
	<% 		
			}
	}
	 %>
		
	</table>
</body>
</html>